package com.mygdx.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.maps.MapRenderer;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.scenes.scene2d.Stage;

/**
 * Created by 80002023 on 2016/7/26.
 */
public class MapTransferScreen implements Screen {


    private Stage mStage;

    private OrthographicCamera mCamera;
    private MapRenderer mMapRenderer;

    private MapTransferPlayer player;
    private MapTransferButton left;
    private MapTransferButton right;

    @Override
    public void show() {

        mStage = new Stage();

        TiledMap map = new TmxMapLoader().load("micro.tmx");

        // 镜头需要设定宽高
        mCamera = new OrthographicCamera(800, 480);
        // 这里默认1个unit单元=1个pixels像素
        // OrthogonalTiledMapRenderer还可以设置单元和像素之间的比例
        mMapRenderer = new OrthogonalTiledMapRenderer(map);
        // 设置镜头的宽和高分别是800,480单元,y方向向上和镜头中间位置
        mCamera.setToOrtho(false, 800, 480);
        mCamera.update();

        player = new MapTransferPlayer();
        player.setPosition(800 / 2, 480 / 2);

        left = new MapTransferButton(-1);
        right = new MapTransferButton(1);

        mStage.addActor(player);
        mStage.addActor(left);
        mStage.addActor(right);
    }

    @Override
    public void render(float delta) {

        // 防止一帧消耗时间过长， 导致卡顿走动现象
        delta = Math.min(0.06f, Gdx.graphics.getDeltaTime());

        // 设置镜头跟随角色
        mCamera.position.x = player.getX();
        // 镜头的更新与设置矩阵到SpriteBatch
        mCamera.update();
        // 舞台更新
        mStage.act(delta);

        // 设置背景
        Gdx.gl.glClearColor(0.7f, 0.7f, 1.0f, 1);
        // 清屏
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        // 设置摄像机
        mMapRenderer.setView(mCamera);
        // 地图绘制
        mMapRenderer.render();
        // 舞台绘制
        mStage.draw();
    }

    @Override
    public void resize(int width, int height) {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {
        mStage.dispose();
    }
}
